// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.NavAfterSellOrder;
import com.fowo.api.model.nav.after.sell.order.*;
import com.fowo.api.service.NavAfterSellOrderService;
import com.fowo.api.service.impl.NavAfterSellOrderServiceImpl;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "订单管理售后工单")
@RequestMapping("/api/nav-after-sell-order")
@RestController
public class NavAfterSellOrderController {

  @Resource
  private NavAfterSellOrderServiceImpl service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<NavAfterSellOrderItemVo>> search(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<NavAfterSellOrderItemVo>>> searchOptions(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(NavAfterSellOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<NavAfterSellOrderItemVo>> getPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<NavAfterSellOrderVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取售后单号相关的编号列表")
  @GetMapping("after-id/{texts}/ids")
  public R<List<Long>> getIdsByAfterId(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByAfterId(nameList));
  }

  @Operation(summary = "获取编号相关的售后单号列表")
  @GetMapping("after-id/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getAfterIdByIds(idList));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody NavAfterSellOrderVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody NavAfterSellOrderVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody NavAfterSellOrderVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<NavAfterSellOrderImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<NavAfterSellOrderImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(
      response,
      "订单管理售后工单导入模版.xlsx"
    );
    InputStream inputStream = new ClassPathResource(
      "templates/NavAfterSellOrder.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('navAfterSellOrderExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    NavAfterSellOrderSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderSubmit')")
  @Operation(summary = "提交")
  @PostMapping("submit")
  public R submit(@RequestBody IdListPo po) {
    return R.run(() -> service.submit(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderProcess')")
  @Operation(summary = "审核")
  @PostMapping("process")
  public R process(@RequestBody IdListPo po) {
    return R.run(() -> service.process(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderCancel')")
  @Operation(summary = "作废")
  @PostMapping("cancel")
  public R cancel(@RequestBody IdListPo po) {
    return R.run(() -> service.cancel(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderIssuRefund')")
  @Operation(summary = "标记退款")
  @PostMapping("issuRefund")
  public R issuRefund(@RequestBody IdListPo po) {
    return R.run(() -> service.issuRefund(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderIssuReturn')")
  @Operation(summary = "标记退货")
  @PostMapping("issuReturn")
  public R issuReturn(@RequestBody IdListPo po) {
    return R.run(() -> service.issuReturn(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderSubInventory')")
  @Operation(summary = "扣库存")
  @PostMapping("subInventory")
  public R subInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.subInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('navAfterSellOrderAddInventory')")
  @Operation(summary = "加库存")
  @PostMapping("addInventory")
  public R addInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.addInventory(idPo.getId()));
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("all-after-order")
  public R<List<NavAfterSellOrderItemVo>> allAfterOrder(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allAfterOrder(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-after-orderPage")
  public R<Page<NavAfterSellOrderItemVo>> allAfterOrderPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allAfterOrderPage(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-after-orderLastUpdateTime")
  public R<Date> allAfterOrderLastUpdateTime(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allAfterOrderLastUpdateTime(search));
  }

  @Operation(summary = "待提交")
  @GetMapping("pending-after-order")
  public R<List<NavAfterSellOrderItemVo>> pendingAfterOrder(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingAfterOrder(search));
  }

  @Operation(summary = "待提交分页")
  @GetMapping("pending-after-orderPage")
  public R<Page<NavAfterSellOrderItemVo>> pendingAfterOrderPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingAfterOrderPage(search));
  }

  @Operation(summary = "待提交最后更新时间")
  @GetMapping("pending-after-orderLastUpdateTime")
  public R<Date> pendingAfterOrderLastUpdateTime(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingAfterOrderLastUpdateTime(search));
  }

  @Operation(summary = "待审批")
  @GetMapping("pending-audit-after-order")
  public R<List<NavAfterSellOrderItemVo>> pendingAuditAfterOrder(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingAuditAfterOrder(search));
  }

  @Operation(summary = "待审批分页")
  @GetMapping("pending-audit-after-orderPage")
  public R<Page<NavAfterSellOrderItemVo>> pendingAuditAfterOrderPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingAuditAfterOrderPage(search));
  }

  @Operation(summary = "待审批最后更新时间")
  @GetMapping("pending-audit-after-orderLastUpdateTime")
  public R<Date> pendingAuditAfterOrderLastUpdateTime(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingAuditAfterOrderLastUpdateTime(search));
  }

  @Operation(summary = "处理中")
  @GetMapping("processing-after-order")
  public R<List<NavAfterSellOrderItemVo>> processingAfterOrder(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processingAfterOrder(search));
  }

  @Operation(summary = "处理中分页")
  @GetMapping("processing-after-orderPage")
  public R<Page<NavAfterSellOrderItemVo>> processingAfterOrderPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processingAfterOrderPage(search));
  }

  @Operation(summary = "处理中最后更新时间")
  @GetMapping("processing-after-orderLastUpdateTime")
  public R<Date> processingAfterOrderLastUpdateTime(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processingAfterOrderLastUpdateTime(search));
  }

  @Operation(summary = "已完成")
  @GetMapping("completed-after-order")
  public R<List<NavAfterSellOrderItemVo>> completedAfterOrder(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedAfterOrder(search));
  }

  @Operation(summary = "已完成分页")
  @GetMapping("completed-after-orderPage")
  public R<Page<NavAfterSellOrderItemVo>> completedAfterOrderPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedAfterOrderPage(search));
  }

  @Operation(summary = "已完成最后更新时间")
  @GetMapping("completed-after-orderLastUpdateTime")
  public R<Date> completedAfterOrderLastUpdateTime(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedAfterOrderLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("cancel-after-order")
  public R<List<NavAfterSellOrderItemVo>> cancelAfterOrder(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancelAfterOrder(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("cancel-after-orderPage")
  public R<Page<NavAfterSellOrderItemVo>> cancelAfterOrderPage(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancelAfterOrderPage(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("cancel-after-orderLastUpdateTime")
  public R<Date> cancelAfterOrderLastUpdateTime(
    NavAfterSellOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancelAfterOrderLastUpdateTime(search));
  }


  @Operation(summary = "客服备注")
  @PostMapping("recordRemark")
  public R recordRemark(@RequestBody CustomNavAfterSellOrderRecordRemarkActionVo po) {
    return R.run(() -> service.customRecordRemark(po));
  }

}
